//
//  GDOtherLoginViewController.m
//  土巴兔
//
//  Created by 千锋 on 16/1/15.
//  Copyright (c) 2016年 IOS1503薛凯. All rights reserved.
//

#import "GDOtherLoginViewController.h"
#import "UMSocial.h"
#import "AFNetworking.h"
#import "Math.h"
#import "NSString+Hash.h"
@interface GDOtherLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation GDOtherLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
//返回
- (IBAction)backClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
//登陆
- (IBAction)Login:(UIButton *)sender {
}
//微博登录
- (IBAction)weiboLogin:(UIButton *)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"Sina:username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount.userName forKey:@"username"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount.usid forKey:@"usid"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount.accessToken forKey:@"token"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount.iconURL forKey:@"iconurl"];
            if (snsAccount.userName!=nil) {
                [self dismissViewControllerAnimated:NO completion:^{
                    [self.delegate closeGuide];
                }];
            }
            
        }});
}
//POST
//http://mobileapi.to8to.com/index.php
//
//username=%E5%B0%BC%E5%85%8B%E5%B0%BC%E5%85%8B%E5%B0%BC%E5%85%8B%E6%9D%A8&systemversion=22&imei=569eb1056c38e1e8&appversion=3.6.0&appostype=1&action=qq&version=2.5&appid=15&to8to_token=&channel=%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83&openid=753D941034AC36C6BC13F622C387A586&model=user&
//
//{
//    "version": "2.5",
//    "action": "UserQqAction",
//    "errorCode": 0,
//    "allRows": 0,
//    "data": {
//        "operation_id": 2,
//        "uid": "5579680",
//        "indentity": 0,
//        "to8to_token": "5579680_bf84a75719c5743bbce42ddf4b5a2d47"
//    }
//}
//QQ登陆
- (IBAction)QQLogin:(UIButton *)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
//            
//            NSLog(@"QQ:username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
//            NSLog(@"____%@",snsAccount);
            
            //将NSData转换成16进制字符串
           NSData* data=[snsAccount.userName dataUsingEncoding:NSUTF8StringEncoding];
           NSMutableString* username=[NSMutableString stringWithString:[[Math convertDataToHexStr:data] uppercaseString]];
            int j=0;
            int num=username.length;
            for (int i=0; i<num; i=i+2) {
                
                [username insertString:@"%" atIndex:i+j];
                j++;
            }
            //腾讯获取数据
//            accessToken = AB8401FCE020E3E74090C46E0237F5EC;
//            expirationDate = "2016-04-16 05:35:51 +0000";
//            iconURL = "http://qzapp.qlogo.cn/qzapp/1105041033/99A6CBD82019CDAFCB18DA54A51C9E16/100";
//            platformName = qzone;
//            profileURL = "";
//            userName = "\U9648\U5e05";
//            usid = 99A6CBD82019CDAFCB18DA54A51C9E16;
    
            //服务器端请求Token
            AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            NSDictionary* paras=@{@"username":username,@"systemversion":@"22",@"imei":@"569eb1056c38e1e8",@"appversion":@"3.6.0",@"appostype":@"1",@"action":@"qq",@"version":@"2.5",@"appid":@"15",@"to8to_token":@"",@"channel":@"%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83",@"openid":@"753D941034AC36C6BC13F622C387A586",@"model":@"user"};
//            =&=&=&=&=&=&=&=&=&=&=&=&
            [manager POST:@"http://mobileapi.to8to.com/index.php" parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                NSDictionary* Todata=[dic objectForKey:@"data"];
                NSLog(@"服务器数据:%@",Todata);
                //将从服务器获取到的数据写入到本地plist文件
                [[NSUserDefaults standardUserDefaults] setObject:snsAccount.userName forKey:@"username"];
                [[NSUserDefaults standardUserDefaults] setObject:[Todata objectForKey:@"to8to_token"] forKey:@"to8to_token"];
                [[NSUserDefaults standardUserDefaults] setObject:snsAccount.iconURL forKey:@"iconurl"];
                [[NSUserDefaults standardUserDefaults] setObject:[Todata objectForKey:@"uid"] forKey:@"uid"];
                if (snsAccount.userName!=nil) {
                    [self dismissViewControllerAnimated:NO completion:^{
                        [self.delegate closeGuide];
                    }];
                }
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                
            }];
          
            
        }});
}

//忘记密码
- (IBAction)ForgetPassWord:(UIButton *)sender {
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_username resignFirstResponder];
    [_password resignFirstResponder];
}

@end
